Shader "snowVF" {

Properties
{
    _Color ("Main Color", Color) = (1,1,1,1)
    _NormalMap ("Normal Map", 2D) = "bump" {}
    _SnowDirection ("SnowDirection", vector) = (0,1,0,1)
	_SnowStength("SnowStength", Range(0,20)) = 10
}
SubShader
{
    Tags {}

    Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct v2f
{
    float4 vertex : SV_POSITION;
    float4 uv : TEXCOORD0;
    // float3 tangent : TEXCOORD1;
    // float3 binormal : TEXCOORD2;
    // float3 normal : TEXCOORD3;

    float4 TtoW0 : TEXCOORD2;  
	float4 TtoW1 : TEXCOORD3;  
	float4 TtoW2 : TEXCOORD4; 
};

sampler2D _NormalMap;
float4 _NormalMap_ST;

float4 _SnowDirection;
fixed4 _Color;
float _SnowStength;


v2f vert (appdata_full v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.uv = v.texcoord;

    // o.tangent = v.tangent.xyz;
    // o.normal = v.normal;
    // o.binormal = normalize(cross(v.normal, v.tangent));

    fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);  
	fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);  
	fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w; 
	
	o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, 1);  
	o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, 1);  
	o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, 1);  


    return o;
}

fixed4 frag (v2f i) : SV_Target
{
    float3 tangentNormal = UnpackNormal(tex2D(_NormalMap, i.uv));
    //float3x3 rotation = float3x3(i.tangent, i.binormal, i.normal);
    //fixed3 worldNormal = normalize(mul(tangentNormal, rotation));

	fixed3 worldNormal = normalize(half3(dot(i.TtoW0.xyz, tangentNormal), dot(i.TtoW1.xyz, tangentNormal), dot(i.TtoW2.xyz, tangentNormal)));

    float relevent = pow(saturate(dot(worldNormal, _SnowDirection.xyz)), 5) * _SnowStength;

    return _Color * relevent;
}

ENDCG
    }
}
    FallBack "Diffuse"
}
